package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.home.dao.ReportDrawingAuditMapper;
import com.eastfair.home.dto.ReportDrawingAuditDTO;
import com.eastfair.home.entity.ReportDrawingAudit;
import com.eastfair.home.service.ReportDrawingAuditService;
import com.eastfair.home.vo.ReportDrawingAuditVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 报馆图纸审核记录
 * </p>
 *
 * @author dq
 * @date 2022-08-08
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportDrawingAuditServiceImpl extends SuperServiceImpl<ReportDrawingAuditMapper, ReportDrawingAudit> implements ReportDrawingAuditService {

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportDrawingAudit> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportDrawingAudit model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<ReportDrawingAuditVO> listVOByReportDrawingCategoryAuditId(Long reportDrawingCategoryAuditId) {
        log.info("listVOByReportDrawingCategoryAuditId - 报馆图纸分类审核ID查询报馆图纸审核VO列表, reportDrawingCategoryAuditId={}", reportDrawingCategoryAuditId);
        ReportDrawingAuditDTO query = new ReportDrawingAuditDTO();
        query.setReportDrawingCategoryAuditId(reportDrawingCategoryAuditId);
        List<ReportDrawingAudit> reportDrawingAudits = listReportDrawingAudit(query);
        if (reportDrawingAudits == null || reportDrawingAudits.isEmpty()) {
            return Collections.emptyList();
        }
        return reportDrawingAudits.stream()
                .map(reportDrawingAudit -> BeanUtil.toBean(reportDrawingAudit, ReportDrawingAuditVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<ReportDrawingAudit> listByReportSpaceAuditId(Long reportSpaceAuditId) {
        log.info("listReportDrawingAuditsByReportSpaceAuditId - 报馆审核ID查询报馆图纸审核列表, reportSpaceAuditId={}", reportSpaceAuditId);
        ReportDrawingAuditDTO query = new ReportDrawingAuditDTO();
        query.setReportSpaceAuditId(reportSpaceAuditId);
        return listReportDrawingAudit(query);
    }

    @Override
    public List<ReportDrawingAudit> listByReportSpaceId(Long reportSpaceId) {
        ReportDrawingAuditDTO query = new ReportDrawingAuditDTO();
        query.setReportSpaceId(reportSpaceId);
        return listReportDrawingAudit(query);
    }

    @Override
    public List<ReportDrawingAudit> listByReportDrawingCategoryAuditIds(List<Long> reportDrawingCategoryAuditIds) {
        ReportDrawingAuditDTO query = new ReportDrawingAuditDTO();
        query.setReportDrawingCategoryAuditIdList(reportDrawingCategoryAuditIds);
        return listReportDrawingAudit(query);
    }

    @Override
    public List<ReportDrawingAudit> listReportDrawingAudit(ReportDrawingAuditDTO query) {
        QueryWrapper<ReportDrawingAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(ReportDrawingAudit::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getReportSpaceId() != null, ReportDrawingAudit::getReportSpaceId, query.getReportSpaceId())
                .eq(query.getReportSpaceAuditId() != null, ReportDrawingAudit::getReportSpaceAuditId, query.getReportSpaceAuditId())
                .eq(query.getReportDrawingCategoryAuditId() != null, ReportDrawingAudit::getReportDrawingCategoryAuditId, query.getReportDrawingCategoryAuditId())
                .in(query.getReportDrawingCategoryAuditIdList() != null && !query.getReportDrawingCategoryAuditIdList().isEmpty(),
                        ReportDrawingAudit::getReportDrawingCategoryAuditId, query.getReportDrawingCategoryAuditIdList())
                .in(query.getDrawingCategoryIdList() != null && !query.getDrawingCategoryIdList().isEmpty(), ReportDrawingAudit::getDrawingCategoryId, query.getDrawingCategoryIdList())
        ;
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveReportDrawingAudit(ReportDrawingAuditDTO reportDrawingAuditDTO) {
        ReportDrawingAudit reportDrawingAudit = BeanUtil.toBean(reportDrawingAuditDTO, ReportDrawingAudit.class);
        return save(reportDrawingAudit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveReportDrawingAuditBatch(List<ReportDrawingAuditDTO> list) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        list.forEach(this::saveReportDrawingAudit);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeBatchByReportSpaceAuditId(Long reportSpaceAuditId) {
        List<ReportDrawingAudit> list = listByReportSpaceAuditId(reportSpaceAuditId);
        return removeByIdsOfLogic(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeBatchByReportSpaceId(Long reportSpaceId) {
        List<ReportDrawingAudit> list = listByReportSpaceId(reportSpaceId);
        if (list == null || list.isEmpty()) {
            return false;
        }
        return removeByIdsOfLogic(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeBatchByReportSpaceIdAndDrawingCategoryId(Long reportSpaceId, List<Long> drawingCategoryIdList) {
        ReportDrawingAuditDTO query = new ReportDrawingAuditDTO();
        query.setReportSpaceId(reportSpaceId);
        query.setDrawingCategoryIdList(drawingCategoryIdList);
        List<ReportDrawingAudit> list = listReportDrawingAudit(query);
        if (list == null || list.isEmpty()) {
            return false;
        }
        return removeByIdsOfLogic(list);
    }
}
